﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data;
using LJTest.entity;

namespace LJTest
{
    class Common
    {
        public const string TRANSACTION_FILE_PATH = "transaction.txt";
        public const string TRANSACTION_FILE_TEMP = "transaction_temp.txt";
        public const string RULE_FILE_PATH = "rule.txt";
        public const string FREQUENCY_FILE_PATH = "frequency.txt";
        public const string FREQUENCY_FILE_TEMP = "frequency_temp.txt";
        public const string CACHE_FILE = "cache.txt";
        public const string CACHE_TEMP_FILE = "cachetemp.txt";
        public const string CACHE_TABLE = "cache";
        public const string CACHE_TEMP_TABLE = "cache_temp";
        public static double totalenergy1 = 100;
        public static double totalenergy2 = 100;

        public static string arraylistTostring(ArrayList a) //仅适用于ArrayList中对象都是string
        {
            string str = "";
            foreach (string s in a)
            {
                str += s + ",";
 
            }
            str = str.Substring(0,str.Length - 1);//去掉最后一个逗号
            return str;
        }
 
        //置信度判断
        public static double getMf()
        {
            double mf=0;
            if (totalenergy2 >= 60) mf = 0.3;
            if (totalenergy2 >= 40 && totalenergy2 < 60) mf = 0.5;
            if (totalenergy2 >= 20 && totalenergy2 < 40) mf = 0.7;
            if (totalenergy2 <20) mf = 0.9;
            return mf;

        }

        //支持度判断
        public static double getSupport()
        {
            double support = 0;
            if (totalenergy2 >= 60) support = 0.2;
            if (totalenergy2 >= 40 && totalenergy2 <60) support  = 0.4;
            if (totalenergy2 >= 20 && totalenergy2 < 40) support  = 0.6;
            if (totalenergy2 < 20) support = 0.8;
            return support;

        }

        //缓存大小计算
        public static double getCacheSize(double size)
        {
            double datasize=0;
            double CacheSize=0;
                for(int i=0;i<=500;i++)
                {
                    datasize += CaculateDataSize( Convert.ToString(i));
                }
                CacheSize = datasize * size ;
                return CacheSize;
        }

        public static int getCacheDataNum(double size)
        {
           double  CacheDataNum = 0;
            CacheDataNum = 1000 * size;
            return Convert.ToInt32 ( CacheDataNum);
        }

        public static void resetFiles()
        {
            File.WriteAllText(TRANSACTION_FILE_PATH, "");
            File.WriteAllText(RULE_FILE_PATH, "");
            File.WriteAllText(FREQUENCY_FILE_PATH, "");
            File.WriteAllText(FREQUENCY_FILE_TEMP, "");
            File.WriteAllText(CACHE_FILE, "");
            File.WriteAllText(CACHE_TEMP_FILE, "");
            File.WriteAllText(TRANSACTION_FILE_TEMP, "");
            SQLiteHelper.ExecuteSql("delete from cache");
            SQLiteHelper.ExecuteSql("delete from cache_temp");
            SQLiteHelper.ExecuteSql("delete from log");
            SQLiteHelper.ExecuteSql("delete from data");
        
           
        }

        //从数据库中读取cache,含时间
        public static ArrayList readCacheFromDB(string cacheTableName)
        {
            string sql = "select * from " + cacheTableName + "";
            ArrayList cacheList = new ArrayList();
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql);
                while (reader.Read())
                {
                    //reader.Read();
                    Cache cache = new Cache();
                    cache.UpdateTime1 = reader.GetString(0);
                    cache.UpdateTime2 = reader.GetString(1);
                    cache.data = reader.GetString(2);
                    cache.Size = Convert.ToDouble( reader.GetString(3));
                    cache.isTemp = reader.GetString(3);
                    cacheList.Add(cache);
                   
                }
                reader.Close();
                return cacheList;
            }
             catch (Exception ex)
             {
                 MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return null;
             }

        }

        //从数据库中读取cache,只含数据
        public static ArrayList readCacheDataFromDB(string cacheTableName)
        {
            string sql = "select * from " + cacheTableName + "";
            ArrayList cacheList = new ArrayList();
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql);
                while (reader.Read())
                {
                    //reader.Read();
                   
                    cacheList.Add(  reader.GetString(2));

                }
                reader.Close();
                return cacheList;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }

        //判断是否已经在缓存中存在
        public static bool checkInCache(string str)
        {
            ArrayList cacheList = readCacheDataFromDB(Common.CACHE_TABLE);
            //string cacheliststr = "cacheList:";
            //foreach (String s in cacheList)
            //{
            //    cacheliststr += s+",";
            //}
            //cacheliststr = cacheliststr.Substring(cacheliststr.Length - 2);
            //cacheliststr += "::" + str;
            //Console.WriteLine(cacheliststr);
            if (cacheList.Contains(str)) return true;
            else return false;
        }


        //从文件中读取cache
        public static ArrayList getCache(string cacheFileName)
        {
            if (File.Exists(cacheFileName))
            {
                try
                {
                    String InLine;
                    ArrayList cache = new ArrayList();
                    ArrayList sub = new ArrayList();

                    using (StreamReader sr = new StreamReader(cacheFileName, Encoding.GetEncoding("gb2312")))
                    {
                        while ((InLine = sr.ReadLine()) != null)
                        {
                            //Console.WriteLine(InLine);
                            if (InLine != null && !InLine.Equals(""))
                            {
                                int firstIndex = InLine.IndexOf("|");


                                string str = InLine.Substring(firstIndex + 1, InLine.Length - 1 - firstIndex);
                                sub.Add(str);
                                cache.Add(sub);
                                sub = new ArrayList();
                            }

                        }
                    }
                    //输出数据并检查是否正确
                    foreach (ArrayList kk in cache)
                    {
                        foreach (string k in kk)
                        {
                            Console.Write(k + '\t');

                        }
                        Console.WriteLine();
                    }
                    

                    Console.WriteLine("以上是读取的记录");
                    return cache;

                }
                catch (Exception ex)
                {
                    Console.WriteLine("读取cache中的记录出错：" + ex.Message);
                    return null;
                }

            }else
            {
                return null;
            }
        }
        public static ArrayList getTransactions(string transationFileName)
        {
            ArrayList Lc = new ArrayList();
            if (File.Exists(transationFileName))
            {
                //Console.WriteLine("文件查找成功");
                try
                {
                    String InLine;

                    ArrayList Lsub = new ArrayList();
                    using (StreamReader sr = new StreamReader(transationFileName, Encoding.GetEncoding("gb2312")))
                    {


                        while ((InLine = sr.ReadLine()) != null)
                        {
                            foreach (string k in InLine.Split(','))
                            {
                                Lsub.Add(k);
                            }
                            Lc.Add(Lsub);
                            Lsub = new ArrayList();
                        }

                    }
                }
                catch (Exception ex)
                {

                }
            }
            return Lc;
        }


        //计算数据项大小
        public static double  CaculateDataSize(string data)
        {
            try
            {
                double x = Convert.ToDouble(data);
                double y;
                y = 0.0095 * x + 0.5;
                return y;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0.0;
            }
        }


    }
}
